package com.jic.manager.impl;

import com.jic.common.base.vo.AdminOperate;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.entity.Certificate;
import com.jic.manager.mapper.CertificateMapper;
import com.jic.manager.request.CertificateRequest;
import com.jic.manager.service.CertificateService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 资质文件表
 * @author : 
 * @email : 
 * @since : 2020-02-26 09:41:21
 * @version : v1.0.0
 */
@Service
public class CertificateServiceImpl implements CertificateService{

    @Resource
    private CertificateMapper certificateMapper;

    @Override
    public long insert(Certificate certificate) {
        long flag = certificateMapper.insert(certificate);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return certificateMapper.delete(id);
    }

    @Override
    public long update(Certificate certificate) {
        return certificateMapper.update(certificate);
    }

    @Override
    public Certificate queryByPrimaryKey(Long id){
        return certificateMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<Certificate> query(Certificate certificate){
        return certificateMapper.query(certificate);
    }

    @Override
    public PageResult<Certificate> queryPage(Certificate certificate, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<Certificate> list = certificateMapper.queryPage(startIndex, pageQuery.getLimit(), certificate);
        long count = certificateMapper.queryPageCount(certificate);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public RestResult<String> editCertificateInfo(Certificate certificate) {
            AdminOperate adminOperate = certificate.getAdminOperate();
            Long id = certificate.getId();
            if (id != null && id !=0){
                certificate.setUpdater(adminOperate.getOperator());
                certificate.setUpdaterName(adminOperate.getOperateName());
                certificate.setDeptId(adminOperate.getDeptId());
                certificateMapper.update(certificate);
            }else {
                //TODO 创建人
                certificate.setCreateTime(new Date());
                certificate.setDeleteFlag(0);
                certificate.setCreator(adminOperate.getOperator());
                certificate.setCreatorName(adminOperate.getOperateName());
                certificate.setDeptId(adminOperate.getDeptId());
                certificateMapper.insert(certificate);
            }
            return RestResult.success("成功");
    }

    @Override
    public RestResult deleteCertificateInfo(Certificate certificate) {
        Long id = certificate.getId();
        if (id !=null){
            certificateMapper.delete(id);
        }
        return RestResult.success("成功");
    }

    @Override
    public void deleteListByTypeAndOrgId(Certificate certificate) {
        certificateMapper.deleteListByTypeAndOrgId(certificate);
    }

    @Override
    public void insertMCertificateList(List<CertificateRequest> mCertificateVos) {
        certificateMapper.insertMCertificateList(mCertificateVos);
    }
}